package systemfnomabss


abstract class Binding 
class StringBindingContext[B](private val bins:List[(String,B)]) {
  
  def + (n: String, b: B) = create( (n,b):: bins)
  def apply(i: Int) : Option[B] = if (!(0<=i && i < bins.length)) 
                                   None else Some(bins(i)._2)                   
                                   
  def isNameBound(n: String): Boolean =  bins exists { case (s, b)  => (s == n) }
  
  def getIndexOfName(n: String) :Int = getIndexOfName(n, bins)
  private def getIndexOfName(n: String,bins: List[(String,B)]) : Int = {
    val index = bins.map {case (n, _) => n} indexOf(n)
    if(index == -1) throw new Exception(n +" is unbound variable") else index
  }
  
  
  def create(bindings:List[(String,B)]) = new StringBindingContext[B](bindings)
  override def toString = bins.toString
}




 






